package com.dmall.opening.up.core;

import java.util.HashMap;
import java.util.Map;

public class Criteria {

    private final Map<String, String[]> criteria;

    private Criteria(Map<String, String[]> parameters) {
        this.criteria = new HashMap<>(parameters);
        parameters.keySet().forEach(k -> {
            final String replacedKey = k.replace("-", "_");
            if (!k.equals(replacedKey)) {
                criteria.put(replacedKey, criteria.get(k));
                criteria.remove(k);
            }
        });
    }

    public static Criteria of(Map<String, String[]> parameters) {
        return new Criteria(parameters);
    }

    public Long page() {
        return Long.valueOf(firstStringValue("page", "1"));
    }

    public Long pageSize() {
        return Long.valueOf(firstStringValue("page_size", "20"));
    }

    private String firstStringValue(String key) {
        return firstStringValue(key, null);
    }

    private String firstStringValue(String key, String defaultValue) {
        final String[] values = criteria.get(key);
        return (values == null || values.length == 0) ? defaultValue : values[0];
    }
}
